home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / mathematica.358 < prev    next >
Text File  |  1992-02-06  |  3KB  |  131 lines

  1. Mathematica communication example pipes
  2.  
  3. Q: How do I communicate with Mathematica using pipes?
  4.  
  5. A: This example is self-contained.
  6.  
  7. #include     <stdio.h>
  8.  
  9.      int     rdchnr, rdchnw, wrtchnr, wrtchnw;
  10.      char    buf[80];
  11.  
  12. main()
  13. {
  14.      int     pid;
  15.      char    *mmapath, *args[5];
  16.  
  17. /*
  18. **   Open pipes for Mathematica to read and write.
  19. **   The parent process writes to the pipe that Mathematica reads,
  20. **   and vice versa.
  21. */
  22.      if (makepipe(&rdchnr,&rdchnw)) exit();
  23.      if (makepipe(&wrtchnr,&wrtchnw)) exit();
  24.  
  25. /*
  26. **   Split off the child process.
  27. */
  28.      pid = fork();
  29.  
  30.      if (pid == 0)
  31.      {
  32. /*
  33. **       *************************************************
  34. **       This is the code for the child process,
  35. **       which sets up for, and then becomes, Mathematica.
  36. **       *************************************************
  37. */
  38.           /* close the unneeded ends of the pipes */
  39.           close(rdchnw);
  40.           close(wrtchnr);
  41.  
  42.           /* set up the arguments for, and the full path name of, Mathematica */
  43.     // mmapath = "/NextApps/Mathematica.app/Kernel/math";
  44.     mmapath = "/usr/bin/math";
  45.     args[0] = mmapath;
  46.     args[1] = "-noprompt";
  47.     args[2] = "-run";
  48.     args[3] = "ResetMedium[\"stdout\", PageWidth->80];$PrePrint=FullForm";
  49.     args[4] = (char *) 0;
  50.  
  51.           /* install the pipes as Mathematica's standard input and output */
  52.           if ( dup2(rdchnr,0) == -1 )
  53.                { perror("Error establishing read channel: "); exit(); }
  54.           if ( dup2(wrtchnw,1) == -1 )
  55.                { perror("Error establishing write channel: "); exit(); }
  56.  
  57.           /* start Mathematica */
  58.           execvp(mmapath,args);
  59.           perror("Error attempting to run Mathematica: ");
  60.      }
  61.      else
  62.      {
  63. /*
  64. **       ****************************************
  65. **       This is the code for the parent process.
  66. **       ****************************************
  67. */
  68.           printf("hi there i got here\n");
  69.       /* close the unneeded ends of the pipes */
  70.           close(rdchnr);
  71.           close(wrtchnw);
  72.  
  73.           /* absorb initialization messages harmlessly */
  74.           fetchresult();
  75.  
  76.           givecmd("Expand[(1+x+y)^5]\n");
  77.           fetchresult();
  78.  
  79.           givecmd("N[Pi,200]\n");
  80.           fetchresult();
  81.  
  82.           /* tell Mathematica to shut down */
  83.           givecmd("Quit[]\\n");
  84.      }
  85. }
  86.  
  87. int makepipe(rd,wrt)
  88.      int     *rd,*wrt;
  89. {
  90.      int     piperesult, fildes[2];
  91.  
  92.      piperesult = pipe(fildes);
  93.      if (piperesult) perror("Pipe creation failed:");
  94.  
  95.      *rd = fildes[0];
  96.      *wrt = fildes[1];
  97.  
  98.      return(piperesult);
  99. }
  100.  
  101. givecmd(cmd)
  102.      char    *cmd;
  103. {
  104.      strcpy(buf,cmd);
  105.      write(rdchnw,buf,strlen(buf));
  106. }
  107.  
  108. fetchresult()
  109. {
  110.      int     count, i, ch;
  111.  
  112. /*
  113. **   Read buffer after buffer of output from Mathematica,
  114. **   until a NEWLINE is seen.  Because Mathematica is printing
  115. **   everything in FullForm, all output is linear, and because
  116. **   PageWidth was set to Infinity, no line breaking happens, so
  117. **   a NEWLINE means the end of a complete output expression.
  118. */
  119.      do {
  120.           count = read(wrtchnr,buf,80);
  121.           for ( i=0 ; i<count ; printf("%c", ch = buf[i++]) ) ;
  122.      } while (ch != '\n');
  123. }
  124.  
  125. QA358
  126.  
  127. Valid for 1.0 
  128. Not checked yet for 2.0 
  129.  
  130.  
  131.